<template>
	<view>
		<view class="status_bar">
			<!-- 这里是状态栏 -->
		</view>
		
		<view class="detail">
			<view class="detailNaver">
				<view @click="backTo" class="backMytask">
					<image src="/static/images/back.png"></image>
				</view>
				<view class="status">
					任务状态
				</view>
				<view></view>
			</view>
			
			<view v-if="detailObject.status == 0 && !type" class="detailCenter">
				<view class="type">
					已领取
				</view>
				<view class="explain">
					请尽快完成任务并提交审核！
				</view>
			</view>
			<view v-if="detailObject.status == 1 && !type" class="detailCenter">
				<view class="type">
					待审核
				</view>
				<view class="explain">
					系统将尽快审核您提交的任务，请及时查看任务状态！
				</view>
			</view>
			<view v-if="detailObject.status == 2 && !type" class="detailCenter">
				<view class="type">
					已拒绝
				</view>
				<view class="explain">
					请慎重提交审核信息，您还有一次重新提交的机会！
				</view>
			</view>
			<view v-if="detailObject.status == 3 && !type" class="detailCenter">
				<view class="type">
					已完成
				</view>
				<view class="explain">
					任务已完成，请及时查看金额变动！
				</view>
			</view>
			<block v-if="type == 'post'">
				<view v-if="detailObject.status == 1" class="detailCenter">
					<view class="type">
						待审核
					</view>
					<view class="explain">
						请审核提交的任务，请及时审核任务！
					</view>
				</view>
				<view v-else-if="detailObject.status == 2" class="detailCenter">
					<view class="type">
						拒绝理由
					</view>
					<view class="explain">
						{{ detailObject.receipt_message }}
					</view>
				</view>
				<view v-else="detailObject.status == 3" class="detailCenter">
					<view class="type">
						审核完成
					</view>
					<view class="explain">
						任务审核已完成，请及时查看金额变动！
					</view>
				</view>
			</block>
		</view>
		
		<view class="detailBody">
			<view class="descriptor">
				<view class="descriptorTop">
					<view class="item">
						<view class="itemLeft">任务名称：</view>
						<view class="itemRight">
							<text>{{ detailObject.task_name }}</text>
							<text v-if="detailObject.type == '1'">关注</text>
							<text v-if="detailObject.type == '2'">点赞</text>
							<text v-if="detailObject.type == '3'">评论</text>
						</view>
					</view>
					<view class="item">
						<view class="itemLeft">任务号：</view>
						<view class="itemRight">{{ detailObject.task_receipt_id }}</view>
					</view>
					<view class="item">
						<view class="itemLeft">领取用户：</view>
						<view class="itemRight">{{ detailObject.nickname }}</view>
					</view>
					<view class="item">
						<view class="itemLeft">领取时间：</view>
						<view class="itemRight">{{ detailObject.receipt_time }}</view>
					</view>
					<view v-if="detailObject.status !== 0" class="item">
						<view class="itemLeft">提交审核时间：</view>
						<view class="itemRight">{{ detailObject.review_time }}</view>
					</view>
					<view v-if="detailObject.status == 3" class="item">
						<view class="itemLeft">审核通过时间：</view>
						<view class="itemRight">{{ detailObject.complete_time }}</view>
					</view>
					<view v-if="detailObject.status == 0 || detailObject.status == 2" style="margin-bottom: 15px;" class="item">
						<view></view>
						<view v-if="detailObject.work_num" @click="openApp(detailObject.platform,detailObject.work_num)" style="color: #FF9244;float: right;" class="itemRights">去完成</view>
						<view v-if="detailObject.work_url" @click="open(detailObject.work_url)" style="color: #FF9244;float: right;" class="itemRights">去完成</view>
					</view>
					<!-- <view style="margin-bottom: 15px;" class="item">
						<view class="itemLeft">任务详情：</view>
						<view @click="goIssueDetail(detailObject.task_id)" style="color: #FF9244;" class="itemRight">查看详情</view>
					</view> -->
					<view class="item" style="align-items: flex-start;">
						<view class="itemLeft">任务图片：</view>
						<view v-if="type == 'post'">
							<view class="itemPhoto">
								<image class="photo" @click="previewImage" :src="detailObject.receipt_pic"></image>
							</view>
						</view>
						<view v-else>
							<view v-if="detailObject.status == 1 || detailObject.status == 3" class="itemPhoto">
								<image class="photo" @click="previewImage" :src="detailObject.receipt_pic"></image>
							</view>
							<view v-if="detailObject.status == 0 || detailObject.status == 2" class="itemPhoto">
								<image v-if="image" v-for="(item,index) in images" :key="index" class="photo"  @click="previewImage" :src="item"></image>
								<image v-if="image" @click="delect" class="delete" src="/static/img/delete_img.png"></image>
								<image v-else class="photo"  @click="changePhoto" src="/static/images/add-picture.png"></image>
							</view>
						</view>
					</view>
				</view>
				
				<view v-if="type == 'post'">
					<view v-if="detailObject.status == 1" class="auditStatus">
						<view class="statusItem">
							<view class="status">
								<text>同意</text>
							</view>
							<view @click="selectStatus" class="select">
								<image v-if="showImg" src="/static/images/pitchOn.png"></image>
								<view v-else></view>
							</view>
						</view>
						
						<view class="statusItem">
							<view class="status">
								<text>拒绝</text>
							</view>
							<view @click="selectStatus" class="select">
								<image v-if="!showImg" src="/static/images/pitchOn.png"></image>
								<view v-else></view>
							</view>
						</view>
						
						<view v-if="!showImg" class="refuseReason">
							<text style="color: red;padding-right:10px;">*</text>
							<textarea v-model="receipt_message" class="task_name" placeholder="请输入拒绝理由" placeholder-style="color: #B3B3B3;" />
						</view>
					</view>
					<view v-else>
						<view v-if="detailObject.status == 2">
							已拒绝
						</view>
						<view v-if="detailObject.status == 3">
							已同意
						</view>
					</view>
				</view>
				
				<view v-else class="descriptorBtm">
					<view class="dsptorBtm">
						<view>
							应收金额：
						</view>
						<view class="price">
							{{ detailObject.price }}车豆
						</view>
					</view>
					<view v-if="detailObject.status == 2" class="dsptorBtm">
						<view>
							拒绝理由：
						</view>
						<view>
							{{ detailObject.receipt_message }}
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view v-if="type == 'post'">
			<view @click="auditTask(detailObject.task_receipt_id)" v-if="detailObject.status == 1" class="gotoBtn">提交</view>
		</view>
		<view v-else>
			<view @click="goAudit(detailObject.task_receipt_id)" v-if="detailObject.status == 0" class="gotoBtn">确认提交</view>
			<view @click="goAudit(detailObject.task_receipt_id)" v-if="detailObject.status == 2" class="gotoBtn">重新提交</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				images:[],
				image:false,
				detailObject:{},
				showImg:true,
				type:'',
				receipt_message:'',
				status:3
			};
		},
		onLoad(e) {
			this.type = e.type
			this.$api.getUserTaskDetail(e.id,res => {
				this.detailObject = res.data
				if(this.detailObject.status == 2) {
					this.detailObject.receipt_pic = ''
				}
				if(this.detailObject.receipt_pic) {
					this.images = this.images.concat(this.detailObject.receipt_pic)
				}
			})
		},
		methods:{
			open(url) {
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android') {
					plus.runtime.openURL(url,function(res) {
						console.log(res)
					},'com.ss.android.ugc.aweme');
				} else if (plus.os.name == 'iOS') {
					plus.runtime.launchApplication({ action: url }, function(e) {
						console.log('Open system default browser failed: ' + e.message);  
					});  
				}
				// #endif
				
				// #ifdef H5
				window.open(url);
				// #endif
			},
			// 同意拒绝
			selectStatus() {
				this.showImg = !this.showImg
				if(this.showImg) {
					this.status = 3
				}else {
					this.status = 2
				}
			},
			// 返回
			backTo() {
				uni.navigateBack();
			},
			// 跳到发布任务详情页面
			goIssueDetail(id) {
				uni.navigateTo({
				    url: `/pages/users/issue_detail/issue_detail?id=${id}`
				});
			},
			// 去完成任务
			openApp(type,appurl) {
				switch (type) {
					case 'douyin':
						appurl = `snssdk1128://aweme/detail/${appurl}`
						break;
					case 'kuaishou':
						appurl = `kwai://work/${appurl}`
						break;
					case 'xiaohongshu':
						appurl = `xhsdiscover://video_feed/${appurl}`
						break;
				}
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android') {
					plus.runtime.openURL(appurl, function(res) {
					 console.log(res);
					});
				} else if (plus.os.name == 'iOS') {
					plus.runtime.launchApplication({ action: appurl }, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});
				}
				// #endif
			
				// #ifdef H5
				window.open(appurl);
				// #endif
			},
			// 上传图片
			changePhoto() {
				this.$api.uploadImage(1, res => {
					this.images = this.images.concat(res.data.path)
					if(this.images.length > 0) {
						this.image = true
					}
				});
			},
			// 删除图片
			delect(){
				uni.showModal({
					title: "提示",
					content: "是否要删除该图片",
					success: (res) => {
						if (res.confirm) {
							this.images.splice(0, 1)
							if(this.images.length == 0) {
								this.image = false
							}
						}
					},
				})
			},
			
			//预览图片
			previewImage() {
				uni.previewImage({
					current: 0,
					urls: this.images
				});
			},
			
			// 任务提交
			goAudit(id) {
				if(this.images.length == 0) {
					this.$common.errorToShow('请上传审核图片')
					return
				}
				let receipt_pic = this.images[0]
				this.$api.postTask({
					id,
					receipt_pic
				},res => {
					if(res.status == 200) {
						let pages = getCurrentPages();  //获取所有页面栈实例列表
						let nowPage = pages[ pages.length - 1];  //当前页页面实例
						let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
						
						// #ifdef APP-PLUS || APP-PLUS-NVUE
						prevPage.$vm.showStyle = 1;
						prevPage.$vm.isover = false
						prevPage.$vm.missionList = [];
						// prevPage.$vm.getMissionList();   //前一个页面方法
						// #endif
									
						// #ifdef H5
						prevPage.showStyle = 1
						prevPage.isover = false
						prevPage.missionList = [];
						// prevPage.getMissionList();   //前一个页面方法
						// #endif
						   //修改上一页data里面的searchVal参数值为1211
						uni.navigateBack({
						    delta: 1
						}); 
					}
					if(res.status == 400) {
						this.$common.errorToShow(res.message) 
					}
				})
			},
			// 审核任务提交
			auditTask(id) {
				this.$api.auditTask({
					id,
					status:this.status,
					receipt_message:this.receipt_message
				},res => {
					this.$common.successToShow('审核成功')
					uni.navigateTo({
					    url: `/pages/users/user-audit/user-audit?id=${this.detailObject.task_id}&&show=1`
					});
				})
			}
		}
	}
</script>

<style lang="less">
.status_bar {
	height: var(--status-bar-height);
	width: 100%;
	background: linear-gradient(90deg, #FF4242, #FF8444);
}

.detail {
	height: 625rpx;
	background: url(../../../static/images/background6.png) no-repeat;
	background-size: 100%;
	padding: 0 20rpx;
	.detailNaver {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 88rpx;
		.backMytask {
			width: 20rpx;
			height: 35rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.status {
			font-size: 35rpx;
			color: #FFFFFF;
		}
	}
	.detailCenter {
		color: #FFFFFF;
		.type {
			font-size: 41rpx;
			margin-bottom: 10rpx;
		}
		.explain {
			font-size: 25rpx;
		}
	}
}

.detailBody {
	position: relative;
	.descriptor {
		position: absolute;
		top: -435rpx;
		left: 0;
		width: 100%;
		height: 939rpx;
		background: url(../../../static/images/background7.png) no-repeat;
		background-size: 100%;
		padding: 80rpx 60rpx 0;
		.descriptorTop {
			border-bottom: 1rpx solid #CCCCCC;
			margin-bottom: 30rpx;
			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 27rpx;
				margin-bottom: 20rpx;
				.itemLeft {
					color: #333333;
				}
				.itemRight {
					color: #999999;
				}
				.itemRights {
					width: 120rpx;
					height: 52rpx;
					background: #eeeeee;
					border-radius: 26rpx;
					line-height: 52rpx;
					text-align: center;
					color: #5e5e5e;
				}
				.itemPhoto {
					position: relative;
					width: 100rpx;
					height: 100rpx;
					.photo {
						width: 100%;
						height: 100%;
					}
					.delete {
						position: absolute;
						top: -15rpx;
						right: -15rpx;
						width: 20rpx;
						height: 20rpx;
						z-index: 10;
					}
				}
			}
		}
		
		.descriptorBtm {
			.dsptorBtm {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 27rpx;
				color: #737373;
				view {
					margin-bottom: 20rpx;
				}
				.price {
					font-size: 40rpx;
					color: #CFAA72;
				}
			}
		}
		
		.auditStatus {
			.statusItem {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 30rpx;
				.status {
					text {
						vertical-align: middle;
					}
				}
				.select {
					view {
						width: 35rpx;
						height: 35rpx;
						border-radius: 50%;
						border: 2rpx solid #999999;
					}
					image {
						width: 35rpx;
						height: 35rpx;
					}
				}
			}
			.refuseReason {
				display: flex;
				height: 150rpx;
				background-color: #ffffff;
				padding: 0 30rpx;
				margin-bottom: 20rpx;
				.task_name {
					width: 100%;
					height: 100rpx;
					background-color: #ECECEC;
				}
			}
		}
	}
		
}

.gotoBtn {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	height: 100rpx;
	background: linear-gradient(90deg, #FF8444, #FF4242);
	font-size: 37rpx;
	color: #FFFFFF;
	text-align: center;
	line-height: 100rpx;
}


</style>
